package com.ndbg.demo.service.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ndbg.demo.model.User;
import com.ndbg.demo.service.user.dao.UserMapper;
import com.ndbg.demo.service.user.service.IUserService;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.springframework.stereotype.Service;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auther
 * @since 2021-09-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    private UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public void add() {
        User user = new User();
        user.setUserName("chen");
        user.setAge(16);
        userMapper.insert(user);
    }

    @Override
    public User get() {
        //RedisUtil.set("111", "2222");
        return userMapper.selectById(3);
    }

    @Override
    public IPage<User> getPage(Page<User> page) {
        return userMapper.getPage(page);
    }

//    public static void main(String[] args) throws Exception{
//        String path = "D:\\test\\出院证明旧.zip";
//        unzip(new File(path));
//        System.out.println("xxxxxxx");
//    }

    public static void unzip(File zipFile) {
        try (ZipArchiveInputStream inputStream = getZipFile(zipFile)) {
            ZipArchiveEntry entry = null;
            while ((entry = inputStream.getNextZipEntry()) != null) {
                if (!entry.isDirectory()) {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int num;
                    while ((num = inputStream.read(buffer)) != -1) {
                        byteArrayOutputStream.write(buffer, 0, num);
                    }
                    byteArrayOutputStream.flush();
                    byte[] bytes = byteArrayOutputStream.toByteArray();
                    String data = new String(bytes, StandardCharsets.UTF_8);
                }
            }
        } catch (Exception e) {
            System.out.println("cccccc");
        }
    }
    private static ZipArchiveInputStream getZipFile(File zipFile) throws Exception {
        return new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    }



    static class Voucher {
        private String voucherBatchCode;
        private double amt;
        private String voucherNo;

        public Voucher(String voucherBatchCode, double amt, String voucherNo) {
            this.voucherBatchCode = voucherBatchCode;
            this.amt = amt;
            this.voucherNo = voucherNo;
        }

        public String getVoucherBatchCode() {
            return voucherBatchCode;
        }

        public double getAmt() {
            return amt;
        }

        public String getVoucherNo() {
            return voucherNo;
        }

        @Override
        public String toString() {
            return "Voucher{" +
                    "voucherBatchCode='" + voucherBatchCode + '\'' +
                    ", amt=" + amt +
                    ", voucherNo='" + voucherNo + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        List<Voucher> vouchers = new ArrayList<>();
        vouchers.add(new Voucher("001", 100.0, "000011"));
        vouchers.add(new Voucher("001", 100.0, "000019"));
        vouchers.add(new Voucher("001", 200.0, "000020"));
        vouchers.add(new Voucher("002", 150.0, "000021"));
        vouchers.add(new Voucher("002", 250.0, "000005"));
        vouchers.add(new Voucher("002", 300.0, "000006"));
        vouchers.add(new Voucher("003", 120.0, "00000007"));
        Collections.sort(vouchers, Comparator.comparing(Voucher::getVoucherNo));

        Map<String, List<Voucher>> groupedVouchers = groupVouchers(vouchers);

        // 打印分组结果
        groupedVouchers.forEach((voucherNo, groupedVoucherList) -> {
            System.out.println("VoucherNo: " + voucherNo);
            groupedVoucherList.forEach(voucher -> System.out.println("\t" + voucher));
        });
    }

    public static boolean isContinuous(String voucherNo1, String voucherNo2) {
        int num1 = Integer.parseInt(voucherNo1);
        int num2 = Integer.parseInt(voucherNo2);
        return num1 + 1 == num2;
    }

    public static Map<String, List<Voucher>> groupVouchers(List<Voucher> vouchers) {
        Map<String, List<Voucher>> groupedVouchers = new HashMap<>();

        // 先对 vouchers 进行排序
        Collections.sort(vouchers, (v1, v2) -> {
            int compareBatchCode = v1.getVoucherBatchCode().compareTo(v2.getVoucherBatchCode());
            if (compareBatchCode != 0) {
                return compareBatchCode;
            }
            return Integer.compare(Integer.parseInt(v1.getVoucherNo()), Integer.parseInt(v2.getVoucherNo()));
        });

        Voucher prevVoucher = null;
        List<Voucher> currentGroup = new ArrayList<>();

        for (Voucher voucher : vouchers) {
            if (prevVoucher == null || isContinuous(prevVoucher.getVoucherNo(), voucher.getVoucherNo()) && prevVoucher.getVoucherBatchCode().equals(voucher.getVoucherBatchCode())) {
                currentGroup.add(voucher);
            } else {
                String groupKey = currentGroup.get(0).getVoucherBatchCode() + "-" + currentGroup.get(0).getVoucherNo() + "-" + prevVoucher.getVoucherNo();
                groupedVouchers.put(groupKey, new ArrayList<>(currentGroup));
                currentGroup.clear();
                currentGroup.add(voucher);
            }
            prevVoucher = voucher;
        }

        // 处理最后一组
        if (!currentGroup.isEmpty()) {
            String groupKey = currentGroup.get(0).getVoucherBatchCode() + "-" + currentGroup.get(0).getVoucherNo() + "-" + prevVoucher.getVoucherNo();
            groupedVouchers.put(groupKey, new ArrayList<>(currentGroup));
        }

        return groupedVouchers;
    }
}
